package edu.chinasoft.servlet;

import java.io.IOException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import java.io.PrintWriter;
import java.util.List;

import javax.servlet.annotation.WebServlet;
import org.apache.log4j.Logger;

import edu.chinasoft.pojo.PageUtilForUser;
import edu.chinasoft.pojo.User;
import edu.chinasoft.service.UserService;
import edu.chinasoft.service.UserServiceImp;

/**
 * 
 * @author cjogng
 * @date 2020年1月17日:上午11:18:09
 * @version V1.0
 * @Description: 查询用户
 */
@WebServlet(name = "myFindUserPageForServlet", value = "/servlet/finduserpageforservlet")
public class FindUserPageForServlet extends HttpServlet {
	private static Logger logger = Logger.getLogger(FindUserPageForServlet.class);
	private UserService userService = new UserServiceImp();

	/**
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindUserPageForServlet >doGet start.......");
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf-8");
		
		// 获取分页的用户
		
		//获取当前页，如果没有，则默认为1
		String currentPageString = request.getParameter("currentPage");
		int currentPage=currentPageString==null?1:Integer.parseInt(currentPageString);
		
		//读取配置文件里的信息，设置关于商品的分页单位
		ServletContext servletContext = this.getServletContext();
		String pageUtilForUserString = servletContext.getInitParameter("PageUtilForUser");
		int pageUtil = Integer.parseInt(pageUtilForUserString);
		
		//调用服务层方法获取用户数据
		PageUtilForUser pageUtilForUser = userService.findUser(currentPage, pageUtil);
		//把数据保存到域   serveltcontext  request   cookie   session
		//              application     request  session   page
		HttpSession session=request.getSession();
		session.setAttribute("alluserpage", pageUtilForUser);
		//跳转到jsp页面中
		response.sendRedirect(""+request.getContextPath()+"/showuser.jsp");
		logger.debug(" FindUserPageForServlet >doGet end.......");
	}

	/**
	 * 
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		logger.debug(" FindUserPageForServlet >doPost start.......");
		this.doGet(request, response);

		logger.debug(" FindUserPageForServlet >doPost end.......");
	}

}